package com.itheima.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.Listener.SubjectExcelListener;
import com.itheima.common.R;
import com.itheima.dao.SysUserDao;
import com.itheima.dao.TbAssignRecordDao;
import com.itheima.dao.TbClueDao;
import com.itheima.dto.ClueQueryDto;
import com.itheima.pojo.SubjectData;
import com.itheima.pojo.SysUser;
import com.itheima.pojo.TbAssignRecord;
import com.itheima.pojo.TbClue;
import com.itheima.service.TbClueService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * 线索(TbClue)表服务实现类
 *
 * @author makejava
 * @since 2022-01-16 15:48:53
 */
@Service("tbClueService")
public class TbClueServiceImpl extends ServiceImpl<TbClueDao, TbClue> implements TbClueService {
    @Resource
    private TbClueDao tbClueDao;

    @Resource
    private SysUserDao sysUserDao;

    @Resource
    private TbAssignRecordDao tbAssignRecordDao;


    @Override
    public R<IPage<TbClue>> page(ClueQueryDto clueQueryDto) {
        if (clueQueryDto.getPage()==null||clueQueryDto.getPageSize()==null) {
            return new R(0,"请求参数不合法",null);
        }

        IPage<TbClue> pageInfo=new Page<>(clueQueryDto.getPage(),clueQueryDto.getPageSize());
        LambdaQueryWrapper<TbClue> lqw=new LambdaQueryWrapper<>();
        //只回显status 1:待跟进   status 2:跟进中
        lqw.eq(TbClue::getStatus,1).or().eq(TbClue::getStatus,2);
        lqw.like(clueQueryDto.getId()!=null,TbClue::getId,clueQueryDto.getId());
        lqw.like(clueQueryDto.getPhone()!=null,TbClue::getPhone,clueQueryDto.getPhone());
        lqw.like(clueQueryDto.getCreateBy()!=null,TbClue::getCreateBy,clueQueryDto.getCreateBy());
        lqw.like(clueQueryDto.getStatus()!=null,TbClue::getStatus,clueQueryDto.getStatus());
        lqw.like(clueQueryDto.getChannel()!=null,TbClue::getChannel,clueQueryDto.getChannel());
        tbClueDao.selectPage(pageInfo,lqw);

        return new R(1,"返回线索分页",pageInfo);

    }

    @Override
    public R add(TbClue tbClue) {
        //添加前请先查询用户是否存在，如果用户存在，则返回用户已存在。
        //添加对象到数据库前请先补全数据
        //线索添加，默认状态 status 为 1 (数据库已有默认值)
        //把数据新增到数据库即可
        TbClue tbClue1=tbClueDao.selectByPhone(tbClue.getPhone());
        if (tbClue1 != null) {
            return new R(0,"手机号已注册!",null);
        }

        tbClue.setStatus("1");//默认待跟进
        tbClue.setCreateBy("user");//
        tbClue.setFalseCount(3);
        tbClue.setCreateTime(new Date());


        int insert = tbClueDao.insert(tbClue);
        if (insert>0) {
            return new R(1,"线索添加成功!",null);
        }
        return new R(0,"线索添加失败!",null);

    }

    @Override
    public R userListByDeptId(Long deptId) {
        LambdaQueryWrapper<SysUser> lqw =new LambdaQueryWrapper<>();
        lqw.eq(SysUser::getDeptId,deptId);
        List<SysUser> userListByDeptId = sysUserDao.selectList(lqw);
        return new R(1,"返回部门对应的用户列表!",userListByDeptId);
    }

    @Override
    public R updateTransfer(Long userId,List<Long>clueIds) {
        if (clueIds.size()>=3) {
            return new R(0,"批量分配线索不能大于3条!",null);

        }
        //int updateTransfer =tbClueDao.updateTransfer(userId);
        SysUser sysUser = sysUserDao.selectById(userId);
        clueIds.forEach(clueId->{
            TbAssignRecord tbAssignRecord =new TbAssignRecord();
            tbAssignRecord.setAssignId(clueId);
            tbAssignRecord.setUserId(userId);
            //tbAssignRecord.setUserName(sysUser.getUserName());
            //tbAssignRecord.setDeptId(sysUser.getDeptId());
            tbAssignRecord.setCreateBy("admin");
            tbAssignRecord.setCreateTime(new Date());
            tbAssignRecord.setLatest("1");
            tbAssignRecord.setStatus("0");
            tbAssignRecord.setType("0");
            tbAssignRecordDao.insert(tbAssignRecord);
        });

            return new R(1,"批量分配线索成功!",null);


    }

    @Override
    public R saveSubject(MultipartFile file, TbClueService subjectService) {
        try{
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new R(0,"文件保存成功!",null);
    }

    @Override
    public R updateStatus(Long id,String status) {

        int updateStatus =tbClueDao.updateStatus(id,status);

        if (updateStatus>0) {
            return new R(1,"修改线索状态成功!",null);
        }
        return new R(0,"修改线索状态失败!",null);
    }

    @Override
    public R updateClueById(TbClue tbClue) {

        int i = tbClueDao.updateById(tbClue);
        if (i >0) {
            return R.success("更新线索成功",null);

        }

        return R.error("更新线索失败");
    }


}
